//
//  UserHomeModel.m
//  CoolTennisBall
//
//  Created by wsl on 16/1/11.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserHomeModel.h"

@implementation UserHomeModel

- (instancetype)initWithDictionary:(NSDictionary *)dic {
    
    self = [super init];
    if (self) {
        _totalCount = dic[@"TotalCount"];
        _totalDuration = dic[@"TotalDuration"];
        _maxSpeed = dic[@"MaxSpeed"];
        _maxScore = dic[@"MaxScore"];
        _avgWeekSport = dic[@"AvgWeekSport"];
        _rankCount = dic[@"Rank"];
        _userID = dic[@"UserID"];
        _headIcon = dic[@"HeadIcon"];
        _userName = dic[@"UserName"];
        _address = dic[@"Address"];
        _medalItems = [self parseMedalItemsArray:dic[@"Medals"]];
    }
    return self;
}

- (NSArray *)parseMedalItemsArray:(NSArray *)medalItemsArray
{
    NSMutableArray *medalItems = [@[] mutableCopy];
    
    for (NSDictionary *medalDictionary in medalItemsArray) {
        MedalItem *medalItem = [[MedalItem alloc] initWithMedalDictionary:medalDictionary];
        [medalItems addObject:medalItem];
        
    }
    
    return medalItems;
}

- (NSString *)readMedalsString
{
    NSMutableArray *medalsString = [@[] mutableCopy];
    for (MedalItem *item in self.medalItems) {
        if (item.firstGet) {
            [medalsString addObject:[@(item.typeIndex) stringValue]];
        }
    }
    
    return [medalsString componentsJoinedByString:@","];
}

@end

@implementation MedalItem

- (instancetype)initWithMedalDictionary:(NSDictionary *)medalDictionary
{
    self = [super init];
    if (self) {
        _createTime = medalDictionary[@"CreateTime"];
        _typeIndex = [medalDictionary[@"Medal"] integerValue];
        _firstGet = [medalDictionary[@"State"] integerValue] == 0;
    }
    return self;
}

@end
